import grace from "../../utils/grace.js"
import util from "../../utils/util.js"
var WxParse = require('../../wxParse/wxParse.js');
grace.page({
      data: {
            imgUrls: [{
                        link: '/pages/index/index',
                        url: '../../image/banner.png'
                  },
                  {
                        link: '/pages/logs/logs',
                        url: '../../image/banner.png'
                  },
                  {
                        link: '/pages/test/test',
                        url: '../../image/banner.png'
                  }
            ],
            indicatorDots: true,
            autoplay: true,
            interval: 5000,
            duration: 1000,
            info: {},
            circular: true,
            indicatorColor: '#999',
            indicatorActiveColor: '#E73032',
            isShow: false,
            goods_id: 0,
            total: {
                  goods_number: 0,
                  free_freight_tips: '',
                  freight_tips: "",
                  order_amount: '',
                  shipping_fee: '',
                  total_amount: ''
            },
            isshare: false, //是否来源于分享
            descHtml: '',
            screenWidth: 375,
            img_width: 150
      },
      onLoad: function(options) {
            wx.showNavigationBarLoading();
            if (this.options.goods_id) {
                  this.$data.goods_id = this.options.goods_id;
            }
            console.log('tset product_detail', this.options);

            if (!wx.getStorageSync('api_token')) {
                  grace.bus.$emit("usre_logout_success");
            } else {
                  this.loadData();
                  this.calcTotal();
            }

            var _this = this;
            wx.getSystemInfo({
                  success: function(res) {
                        _this.setData({
                              screenHeight: res.windowHeight,
                              screenWidth: res.windowWidth,
                              img_width: res.windowWidth / 2.5
                        });
                  }
            });

            grace.bus.$on("update_cart_event", () => {

            });

            this.$bus.$on("auth_success_event", (data) => {
                  this.loadData();
                  this.calcTotal();
            });
      },
      loadData: function() {
            var that = this;
            var goods_id = this.$data.goods_id;
            this.$http.get('api/goods/detail', {
                  goods_id: goods_id
            }).then((res) => {
                  this.$data.info = res;
                  if (res && res['goods_desc']) {
                        WxParse.wxParse('descHtml', 'html', res['goods_desc'], that, 0);
                  }
            });
      },
      calcTotal: function() {
            this.$http.get('api/cart/statistics', {}).then((res) => {
                  this.$data.total = res;
            })
      },
      openCart: function() {
            wx.switchTab({
                  url: '../cart/cart',
            })
      },
      prevewImage: function(e) {
            let index = e.currentTarget.dataset.index;
            let gallery_list = this.$data.info.gallery_list;
            let curr_org_url = gallery_list[index]['img_origin_url'];
            let org_arr = new Array();
            for (let i = 0, len = gallery_list.length; i < len; i++) {
                  let temp = gallery_list[i];
                  if (temp) {
                        org_arr.push(temp['img_origin_url']);
                  }
            }

            wx.previewImage({
                  current: curr_org_url, // 当前显示图片的http链接
                  urls: org_arr // 需要预览的图片http链接列表
            })
      },
      openDetail: function(e) {
            let menu = e.currentTarget.dataset.menu;
            wx.navigateTo({
                  url: '../menu_recommend/menu_recommend?menu_id=' + menu,
            })
      },
      openSubmit: function(e) {
            let that = this;
            let goods_id = e.currentTarget.dataset.goods;
            this.$http.post('api/cart/add_goods', {
                  goods_vals: goods_id,
                  is_cumulation: 0,
                  goods_number: 1
            }).then((res) => {
                  grace.bus.$emit("update_cart_event");
                  let cart_vals = res.cart_vals;
                  wx.navigateTo({
                        url: '../cart_pay/cart_pay?cart_vals=' + cart_vals
                  });
            });
      },
      addToCart: function(e) {
            let that = this;
            let goods_id = e.currentTarget.dataset.goods;

            this.$http.post('api/cart/add_goods', {
                  goods_vals: goods_id
            }).then((res) => {
                  this.toast(res.message);
                  setTimeout(function() {
                        that.calcTotal();
                        grace.bus.$emit("update_cart_event");
                  }, 500);
            });
      },
      onShareAppMessage: function() {
            let user_id = wx.getStorageSync('user_id');
            let from_store = wx.getStorageSync('current_store');
            let share_url = 'pages/product_details/product_details?isshare=1&from_user=' + user_id + '&from_store=' + from_store + '&goods_id=' + this.$data.goods_id;
            return {
                  title: this.$data.info.goods_name,
                  path: share_url,
                  success: function(res) {

                  },
                  fail: function(res) {
                        // 转发失败
                  }
            }
      }
})